import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:get/get.dart';
import 'package:yh_core_flutter_project/project/router/app_routers.dart';
import '../../../common/constant/icon.dart';
import '../../../common/constant/styles.dart';
import 'package:app_core/app_core.dart';
import '../../../common/constant/colors.dart';
import '../../../common/intl/index.dart';
import 'sign_up_logic.dart';

class SignUpPage extends StatelessWidget {
  SignUpPage({super.key});

  final controller = Get.put(SignUpLogic());

  @override
  Widget build(BuildContext context) {
    return DismissKeyboardWidget(
      child: Scaffold(
        backgroundColor: AppColor.white,
        appBar: MyAppBar(
          '注册',
          actions: [
            AppButton.text(
              width: 44,
              foregroundColor: Colors.green,
              style: TextStyle(fontSize: 16, fontWeight: FontWeight.w500),
              text: '跳过',
              onTap: () {
                Get.log('跳过');
                Get.toNamed(AppRouters.baseTest.r);
              },
            ),
          ],
        ),
        body: SingleChildScrollView(
          padding: EdgeInsets.symmetric(horizontal: 10),
          child: ConstrainedBox(
            constraints: BoxConstraints(minHeight: 1000),
            child: IntrinsicHeight(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Padding(
                    padding: EdgeInsets.only(top: 20),
                    child: Column(
                      children: [
                        Container(
                          width: 80,
                          height: 80,
                          decoration: const BoxDecoration(shape: BoxShape.circle, color: AppColor.logoBackground),
                          child: const Icon(Icons.person_add, color: AppColor.white, size: 40),
                        ),
                        const SizedBox(height: 16),
                        Text('创建账号', style: TextStyle(color: AppColor.logoBackground)),
                        const SizedBox(height: 12),
                        // iTAGKit 文字
                        RichText(
                          text: const TextSpan(
                            style: TextStyle(color: Color(0xFF1A1A1A)),
                            children: [
                              TextSpan(text: '欢迎使用 ', style: TextStyle(fontSize: 20, fontWeight: FontWeight.normal)),
                              TextSpan(text: 'iTAGKit', style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold)),
                            ],
                          ),
                        ),
                      ],
                    ),
                  ),
                  SizedBox(height: 48),
                  _buildEmail(),
                  SizedBox(height: 24),
                  _buildPwd(),
                  SizedBox(height: 24),
                  _buildConfirmPwd(),
                  SizedBox(height: 20),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: [
                      AppGesture(
                        onTap: () {
                          // Get.toNamed(AppRouters.emialCode.r);
                        },
                        child: Padding(padding: EdgeInsets.symmetric(vertical: 4), child: Text(LoginIntl.forgotPassword.tr, style: AppStyles.s16)),
                      ),
                      SizedBox(width: 16),
                    ],
                  ),
                  SizedBox(height: 24),
                  _buildPrivacy(),
                  _buildLoginButton(),
                  SizedBox(height: 16),
                  AppGesture(
                    padding: EdgeInsets.symmetric(vertical: 10),
                    // onTap: () => Get.toNamed(AppRouters.signUp.r),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Padding(padding: EdgeInsets.symmetric(vertical: 10), child: Text('我还没有账号', style: AppStyles.s14)),
                        AppGesture(
                          padding: EdgeInsets.symmetric(horizontal: 4),
                          onTap: () {
                            Get.log('我还没有账号');
                          },
                          child: Image.asset(AppIcon.noAccount, width: 54),
                        ),
                      ],
                    ),
                  ),
                  // SizedBox(height: AppScreenInfo.safeAreaBottom.value),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildEmail() {
    return Padding(
      padding: EdgeInsets.only(left: 16, right: 16),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text('电子邮箱', style: TextStyle(color: AppColor.textPrimary, fontSize: 14)),
          SizedBox(height: 5),
          GetBuilder<SignUpLogic>(
            id: SignUpUpdateKey.email,
            builder: (ref) {
              return AppTextField.prefixInput(
                focusNode: controller.emailFocusNode,
                keyboardType: TextInputType.emailAddress,
                controller: controller.emailController,
                // obscureText: !controller.showPassword,
                hintText: '请输入电子邮箱',
                // 移除border，添加背景色
                border: null,
                bgColor: AppColor.inputBackground,
                // 调整高度和内边距
                height: 50,
                contentPadding: EdgeInsets.symmetric(horizontal: 6),
                prefixIcon: Padding(padding: AppSpaces.edgeSm, child: Icon(Icons.verified_user_outlined, color: AppColor.primary, size: 20)),
                suffixIcon: Row(
                  mainAxisAlignment: MainAxisAlignment.end, // 设置为末端对齐
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    if (controller.emailController.text.isNotEmpty && controller.emailFocusNode.hasFocus)
                      AppGesture(
                        padding: EdgeInsets.symmetric(horizontal: 4),
                        onTap: () {
                          controller.emailController.clear();
                        },
                        child: Image.asset(AppIcon.icClose, width: 20),
                      ),
                    // if (!controller.isEmailValid) _buildError(LoginIntl.enterValidEmail.tr),
                  ],
                ),
              );
            },
          ),
        ],
      ),
    );
  }

  // 密码框
  Widget _buildPwd() {
    return Padding(
      padding: EdgeInsets.only(left: 16, right: 16),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text('密码', style: TextStyle(color: AppColor.textPrimary, fontSize: 14)),
          SizedBox(height: 5),
          GetBuilder<SignUpLogic>(
            id: SignUpUpdateKey.password,
            builder: (ref) {
              return AppTextField.prefixInput(
                focusNode: controller.passwordFocusNode,
                keyboardType: TextInputType.visiblePassword,
                controller: controller.passwordController,
                obscureText: !controller.showPassword,
                hintText: '请输入密码',
                // 移除border，添加背景色
                border: null,
                bgColor: AppColor.inputBackground,
                // 调整高度和内边距
                height: 50,
                contentPadding: EdgeInsets.symmetric(horizontal: 6),
                prefixIcon: Padding(padding: AppSpaces.edgeSm, child: Icon(Icons.verified_user_outlined, color: AppColor.primary, size: 20)),
                suffixIcon: Row(
                  mainAxisAlignment: MainAxisAlignment.end, // 设置为末端对齐
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    if (controller.passwordController.text.isNotEmpty && controller.passwordFocusNode.hasFocus)
                      AppGesture(
                        padding: EdgeInsets.symmetric(horizontal: 4),
                        onTap: () {
                          controller.passwordController.clear();
                        },
                        child: Image.asset(AppIcon.icClose, width: 20),
                      ),
                    AppGesture(
                      padding: EdgeInsets.symmetric(horizontal: 4),
                      onTap: controller.togglePasswordVisibility,
                      child: SvgPicture.asset(controller.showPassword ? AppIcon.icPwdShow : AppIcon.icPwdHide),
                    ),
                  ],
                ),
              );
            },
          ),
        ],
      ),
    );
  }

  // 再次确定密码
  Widget _buildConfirmPwd() {
    return Padding(
      padding: EdgeInsets.only(left: 16, right: 16),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text('确认密码', style: TextStyle(color: AppColor.textPrimary, fontSize: 14)),
          SizedBox(height: 5),
          GetBuilder<SignUpLogic>(
            id: SignUpUpdateKey.confirmPassword,
            builder: (ref) {
              return AppTextField.prefixInput(
                focusNode: controller.confirmPasswordFocusNode,
                keyboardType: TextInputType.visiblePassword,
                controller: controller.confirmPasswordController,
                obscureText: !controller.showConfirmPassword,
                hintText: '请输入密码',
                // 移除border，添加背景色
                border: null,
                bgColor: AppColor.inputBackground,
                // 调整高度和内边距
                height: 50,
                contentPadding: EdgeInsets.symmetric(horizontal: 6),
                prefixIcon: Padding(padding: AppSpaces.edgeSm, child: Icon(Icons.verified_user_outlined, color: AppColor.primary, size: 20)),
                suffixIcon: Row(
                  mainAxisAlignment: MainAxisAlignment.end, // 设置为末端对齐
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    if (controller.confirmPasswordController.text.isNotEmpty && controller.confirmPasswordFocusNode.hasFocus)
                      AppGesture(
                        padding: EdgeInsets.symmetric(horizontal: 4),
                        onTap: () {
                          controller.confirmPasswordController.clear();
                        },
                        child: Image.asset(AppIcon.icClose, width: 20),
                      ),
                    AppGesture(
                      padding: EdgeInsets.symmetric(horizontal: 4),
                      onTap: controller.toggleConfirmPasswordVisibility,
                      child: SvgPicture.asset(controller.showConfirmPassword ? AppIcon.icPwdShow : AppIcon.icPwdHide),
                    ),
                  ],
                ),
              );
            },
          ),
        ],
      ),
    );
  }

  // 验证错误提示
  Widget _buildError(String errorText) {
    return Padding(padding: EdgeInsets.only(left: 20, right: 20, top: 8), child: Text(errorText, style: AppStyles.s12.copyWith(color: AppColor.textError)));
  }

  // 隐私政策
  Widget _buildPrivacy() {
    return GetBuilder<SignUpLogic>(
      id: SignUpUpdateKey.privacy,
      builder: (_) {
        return Padding(
          padding: EdgeInsets.symmetric(horizontal: 8, vertical: 8),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              AppGesture(
                onTap: controller.toggleAgree,
                child: Padding(
                  padding: EdgeInsets.all(6),
                  child: SvgPicture.asset(!controller.agree ? AppIcon.icCircleSelectN : AppIcon.icCircleSelectS, width: 16, height: 16),
                ),
              ),
              Expanded(
                child: Text.rich(
                  TextSpan(
                    text: LoginIntl.agree.tr,
                    style: AppStyles.s14w5.copyWith(color: AppColor.textSecondary),
                    children: [
                      TextSpan(
                        text: LoginIntl.privacyPolicy.tr,
                        style: AppStyles.s14w5.copyWith(color: AppColor.textPrimary),
                        recognizer:
                            TapGestureRecognizer()
                              ..onTap = () async {
                                // final result = await Get.dialog<bool>(ServiceTermsDialog(), barrierDismissible: false);
                              },
                      ),
                      TextSpan(text: "和", style: AppStyles.s14.copyWith(color: AppColor.textSecondary)),
                      TextSpan(
                        text: LoginIntl.termsOfService.tr,
                        style: AppStyles.s14w5.copyWith(color: AppColor.textPrimary),
                        recognizer:
                            TapGestureRecognizer()
                              ..onTap = () async {
                                // final result = await Get.dialog<bool>(ServiceTermsDialog(), barrierDismissible: false);
                              },
                      ),
                    ],
                  ),
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  // 注册
  Widget _buildLoginButton() {
    return Container(
      margin: EdgeInsets.only(left: 16, right: 16),
      child: GetBuilder<SignUpLogic>(
        id: SignUpUpdateKey.button,
        builder: (controller) {
          return AppButton.text(
            text: LoginIntl.signUp.tr,
            onTap: controller.sendCode,
            enable: controller.canSubmit,
            // backgroundColor: AppColor.btnColor,
            // disableBackgroundColor: AppColor.btnDisableColor,
            backgroundColor: AppColor.scaffoldBackground,
            side: const BorderSide(color: AppColor.textSecondary, width: 1),
            style: AppStyles.s16w5,
          );
        },
      ),
    );
  }
}
